package com.sphd.Restaurant.presenter;

import android.util.Log;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.sphd.Restaurant.infos.CardInfo;
import com.sphd.Restaurant.infos.IntegralInfo;
import com.sphd.Restaurant.infos.PromotionsInfo;
import com.sphd.Restaurant.infos.RestaurantInfo;
import com.sphd.Restaurant.infos.TopupInfo;
import com.sphd.Restaurant.interfFace.VolleyCallBack;
import com.sphd.Restaurant.utils.JsonUtil;
import com.sphd.Restaurant.utils.VolleyErrorHelper;
import com.sphd.Restaurant.views.RestaurantPageView;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/10/8.
 */

public class RestaurantPageP extends BasePresenter implements VolleyCallBack<JSONObject> {
    RestaurantPageView restaurantPageView;

    public RestaurantPageP(RestaurantPageView restaurantPageView) {
        this.restaurantPageView = restaurantPageView;
    }

    @Override
    public void addData(String url) {
        restaurantPageView.showdialog();
        sendJsonObjectUrl(url, this);
    }


    @Override
    public void onResponse(JSONObject jsonObject) {
        try {
            List<CardInfo> cardInfos = JsonUtil.getListFromString(jsonObject.getString("cardInfo"), CardInfo.class);
            List<IntegralInfo> integralInfos = JsonUtil.getListFromString(jsonObject.getString("integralInfo"), IntegralInfo.class);
            List<TopupInfo> topupInfos = JsonUtil.getListFromString(jsonObject.getString("topupInfo"), TopupInfo.class);
            List<PromotionsInfo> promotionsInfos = JsonUtil.getListFromString(jsonObject.getString("promotionsInfo"), PromotionsInfo.class);
            restaurantPageView.sendLists(cardInfos,integralInfos,topupInfos,promotionsInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        restaurantPageView.dissmissdialog();
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(context, VolleyErrorHelper.getMessage(error),Toast.LENGTH_SHORT).show();
        restaurantPageView.dissmissdialog();
    }

    public void getCgMessage(String url){
        restaurantPageView.showdialog();
        sendJsonObjectUrl(url, new VolleyCallBack<JSONObject>() {
            @Override
            public void onResponse(JSONObject object) {
                try {
                    RestaurantInfo restaurantInfo =JsonUtil.getObjectFromString(object.getString("res"),RestaurantInfo.class);
                    restaurantPageView.sendRestaurantInfo(restaurantInfo);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.e("message====",object.toString());
                restaurantPageView.dissmissdialog();
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context,VolleyErrorHelper.getMessage(error),Toast.LENGTH_SHORT).show();
                restaurantPageView.dissmissdialog();
            }
        });
    }


    public void addToVip(String url) {
        restaurantPageView.showdialog();
        sendJsonObjectUrl(url, new VolleyCallBack<JSONObject>() {
            @Override
            public void onResponse(JSONObject object) {
                restaurantPageView.dissmissdialog();
                Log.e("ob====",object.toString());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                restaurantPageView.dissmissdialog();Toast.makeText(context,VolleyErrorHelper.getMessage(error),Toast.LENGTH_SHORT).show();
            }
        });
    }
}
